home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0036_Conversion to Base 36.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  2KB  |  41 lines

  1. (*
  2. From: TIM MCKAY
  3. Subj: RE: COVERTING TO BASE 36
  4.  
  5.  JF> Can someone please show me how I would convert a base 10 number to
  6.  JF> base 36? (The one used by RIP)
  7. *)
  8.  
  9. program convertbase;
  10.  
  11.   const B: integer = 36;       { B = the base to convert to }
  12.           S: string  = '';       { S = the string representation of the
  13.                                      result }
  14.                                              done: boolean = false;
  15.  
  16.   var   X, I, F: integer;      { X = the original base 10 number
  17.                                  I = the integer portion of the result
  18.                                  F = the fractional portion of the
  19.                                      result }
  20.                                              R: real;               { R = the
  21. intermediate real result }
  22.  
  23.   begin
  24.     readln(X);                 { Get original base 10 number }
  25.     R:=X;
  26.     while (not done) do begin  { This loop continues to divide the     }
  27.           R:= R/B;                 { result by the base until it reaches 0 }
  28.           I:= int (R);             { The integer portion of the result is  }
  29.           R:= I;                   { reassigned to R                    }
  30.           F:= frac(R) * B;         { The fractional portion is converted to}
  31.           if f<10 then begin       { an integer remainder of the original  }
  32.             S:=chr(f+$30) + S;     { base and converted to a character to  }
  33.           end else begin           { be added to the string representation }
  34.          S:=chr(f+$37) + S;
  35.       end;
  36.       if R<=0 then done:=true; { When R reaches 0 then you're done     }
  37.           end;
  38.     writeln(S);
  39.   end.
  40.  
  41.